package com.linked.mystyle.news.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.mystyle.news.bean.param.NewsPageParam;
import com.linked.mystyle.news.bean.po.NewsInfoPO;
import com.linked.mystyle.news.service.INewsInfoService;
import com.linked.universal.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author :dbq
 * @date : 2022/10/28 15:16
 */
@RestController
@RequestMapping("news")
public class NewsController {

    private final static Logger logger = LoggerFactory.getLogger(NewsController.class);

    private ObjectMapper mapper;
    private final INewsInfoService newsInfoService;

    @Autowired
    public NewsController(ObjectMapper mapper, INewsInfoService newsInfoService) {
        this.mapper = mapper;
        this.newsInfoService = newsInfoService;
    }

    @PostMapping("queryNewsPage")
    Result queryNewsPage(@RequestBody NewsPageParam param) {
        IPage<NewsInfoPO> ret = null;
        try {
            ret = newsInfoService.queryNewsPage(param);
        } catch (Exception e) {
            logger.error("查询新闻页面接口异常！", e);
            return Result.error("查询新闻页面接口异常");
        }
        return Result.success(ret);
    }

    @PostMapping("addNews")
    Result addNews(@RequestBody NewsInfoPO param) {

        boolean ret = false;
        try {
            ret = newsInfoService.addNews(param);
        } catch (Exception e) {
            logger.error("添加新闻接口异常！", e);
        }
        return ret ? Result.success("添加新闻成功！") : Result.ok(false, "添加新闻失败！");
    }

    /**
     * 点赞
     */
//    @GetMapping("doLikes")
//    Result doLikes(@RequestParam("newsId") String newsId) {
//
//        logger.info("新闻id:" + newsId);
//        boolean ret = false;
//
//        try {
//            ret = newsInfoService.doLikes(newsId);
//        } catch (Exception e) {
//            logger.error("点赞接口异常", e);
//        }
//        return ret ? Result.ok("点赞成功") : Result.error("点赞失败");
//    }

    /**
     * 点赞
     */
    @GetMapping("doLikes")
    public boolean doLikes(@RequestParam("newsId") String newsId) {


        logger.info("新闻id:" + newsId);
        boolean ret = false;
        try {
            ret = newsInfoService.doLikes(newsId);
        } catch (Exception e) {
            logger.error("点赞接口异常", e);
            throw new RuntimeException(e);
        }

        return ret;
    }
}
